package com.agp.mapper;

import com.agp.dto.AncientTreePageQueryDTO;
import com.agp.dto.MessageQueryDTO;
import com.agp.dto.TreeUpdateDTO;
import com.agp.entity.AncientTree;
import com.agp.entity.HealthRecord;
import com.agp.vo.TreeFamilyCountVO;
import com.agp.vo.TreeGenusCountVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface TreeMapper {
   /* // 插入古树信息
    @Insert("insert into ancient_tree(chinese_name,scientific_name,family_name," +
            "genus_name,estimated_age,protection_level,gps_longitude,gps_latitude,location_address," +
            "description,create_time,update_time) " +
            "values(#{chineseName},#{scientificName},#{familyName},#{genusName},#{estimatedAge}," +
            "#{protectionLevel},#{gpsLongitude},#{gpsLatitude},#{locationAddress}," +
            "#{description},#{createTime},#{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "treeId")
    void insert(AncientTree tree);*/


    // 古树分页查询
    Page<AncientTree> page(AncientTreePageQueryDTO pageQueryDTO);

    // 古树标记信息查询
    @Select("select tree_id,estimated_age,chinese_name,description,gps_longitude,gps_latitude from ancient_tree")
    List<AncientTree> selectMarker();


    // 古树更新功能
    Integer update(TreeUpdateDTO treeUpdateDTO);

    // 古树删除功能，实现批量删除，单个删除两种方式
    Integer delete(Integer[] ids);

    // 古树科名数量统计功能
    @Select("select family_name,count(1) as count from ancient_tree group by family_name")
    List<TreeFamilyCountVO> selectTreeFamilyCount();

    // 古树属名数量统计功能
    @Select("select genus_name,count(1) as count from ancient_tree group by genus_name")
    List<TreeGenusCountVO> selectTreeGenusCount();

    // 古树详情查询功能
    @Select("select * from ancient_tree where tree_id=#{treeId}")
    AncientTree getTreeById(Integer treeId);

    // 批量插入功能
    void insertBatch(List<AncientTree> ancientTreeList);


    // 健康记录功能
    void insertHealthyRecord(HealthRecord healthRecord);

    // 查询健康状态功能
    @Select("SELECT health_status " +
            "FROM tree_health_record " +
            "WHERE tree_id = #{treeId} " +
            "ORDER BY health_status DESC " +
            "LIMIT 1")
    Integer selectHealthStatus(Integer treeId);

    // 获取健康记录功能
   /* @Select("SELECT record_id, tree_id, check_date, health_status, unhealthy_description, is_resolved, photo_urls " +
            "FROM tree_health_record " +
            "WHERE tree_id = #{treeId} " +
            "ORDER BY check_date DESC")*/
    List<HealthRecord> getHealthyRecord(MessageQueryDTO messageQueryDTO);

    @Update( "UPDATE tree_health_record set is_resolved = #{isResolved} where record_id=#{recordId}")
    void updateHealthRecordStatus(HealthRecord healthRecord);
}
